package gmmxEditor;

import gmmxEditor.GUI.CButton;
import gmmxEditor.GUI.ColorPicker;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

public class PreferencesPane extends JDialog {

	private static final long	serialVersionUID	= 1706139991293654430L;
	private final JPanel		contentPanel		= new JPanel();
	private JTextField			squareSize;

	private ColorPicker			colorPicker;

	/**
	 * Create the dialog.
	 */
	public PreferencesPane(Component parent) {
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		this.setBounds(100, 100, 450, 300);
		this.getContentPane().setLayout(new BorderLayout());
		this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.getContentPane().add(this.contentPanel, BorderLayout.CENTER);
		this.contentPanel.setLayout(null);
		this.setLocationRelativeTo(parent);

		this.squareSize = new JTextField(Preferences.getInstance().getSquareSide() + "");
		this.squareSize.setBounds(265, 6, 134, 28);
		this.contentPanel.add(this.squareSize);
		this.squareSize.setColumns(10);

		JLabel lblNewLabel = new JLabel("Square size:");
		lblNewLabel.setBounds(6, 12, 247, 16);
		this.contentPanel.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("px");
		lblNewLabel_1.setBounds(411, 12, 33, 16);
		this.contentPanel.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("Map pointer color:");
		lblNewLabel_2.setBounds(6, 45, 247, 16);
		this.contentPanel.add(lblNewLabel_2);

		final JButton btnNewButton = new CButton();
		btnNewButton.setBackground(Preferences.getInstance().getSquareColor());
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				PreferencesPane.this.colorPicker = new ColorPicker(Preferences.getInstance().getSquareColor());
				if (JOptionPane.showConfirmDialog(null, PreferencesPane.this.colorPicker, "Edit Player", JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.PLAIN_MESSAGE) == JOptionPane.YES_OPTION) {
					Preferences.getInstance().setSquareColor(PreferencesPane.this.colorPicker.selectedColor);
					btnNewButton.setBackground(PreferencesPane.this.colorPicker.selectedColor);
				}
			}
		});
		btnNewButton.setBounds(315, 40, 39, 29);
		this.contentPanel.add(btnNewButton);

		JLabel lblMapLinesColor = new JLabel("Map lines color:");
		lblMapLinesColor.setBounds(6, 78, 247, 16);
		this.contentPanel.add(lblMapLinesColor);

		final CButton btnNewButton_1 = new CButton();
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				PreferencesPane.this.colorPicker = new ColorPicker(Preferences.getInstance().getLinesColor());
				if (JOptionPane.showConfirmDialog(null, PreferencesPane.this.colorPicker, "Edit Player", JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.PLAIN_MESSAGE) == JOptionPane.YES_OPTION) {
					Preferences.getInstance().setLinesColor(PreferencesPane.this.colorPicker.selectedColor);
					btnNewButton_1.setBackground(PreferencesPane.this.colorPicker.selectedColor);
				}
			}
		});
		btnNewButton_1.setBackground(Preferences.getInstance().getLinesColor());
		btnNewButton_1.setBounds(315, 73, 39, 29);
		this.contentPanel.add(btnNewButton_1);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			this.getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						Preferences.getInstance().setSquareSide(Integer.parseInt(PreferencesPane.this.squareSize.getText()));

						Preferences.getInstance().save();

						PreferencesPane.this.dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				this.getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						PreferencesPane.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		this.setVisible(true);
	}
}
